﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Collections.ObjectModel;
using Skynet.BusinessEntities;
using System.ComponentModel;
using Infrastructure.Aspect.Extensions;
using Infrastructure.WpfClient.Extensions;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Windows.Data;
using Skynet.BusinessLogic.BusinessComponents;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection;
using Infrastructure.WpfClient.Services.Validation;
using System.Windows;
namespace Skynet.Administracion.Views.ExamCategoryMgm
{
    public class ExamCategoryMgmPresenter : Infrastructure.WpfClient.Views.Master.MasterPresenter<IExamCategoryMgmView>
    {
        [Dependency]
        public IBCExams BCExams { get; set; }

        [Dependency]
        public IBCExamQueries BCExamQueries { get; set;  }

        [Dependency]
        public IValidationService ValidationService { get; set; }


        private ObservableCollection<ExamCategory> _Categorias = null;

        public ICollectionView Categorias { get; set; }

        public DelegateCommand<ExamCategory> DeleteCommand { get; set; }
        public DelegateCommand<ExamCategory> AddCommand { get; set; }

        public ExamCategoryMgmPresenter(IUnityContainer container) : base(container)
        {
            this.TitleView = "Category Mgm";
            DeleteCommand = new DelegateCommand<ExamCategory>(C => this.Delete(C), C => this.Categorias != null && this.Categorias.CurrentItem != null);
            AddCommand = new DelegateCommand<ExamCategory>(C => this.Add(C));                    
        }

        public override void OnViewReady()
        {
            
            this._Categorias = this.BCExamQueries.SearchExamCategories().ToObservableCollection(); 
            this._Categorias.ExecuteAction((INotifyPropertyChanged  C)=>C.PropertyChanged+=Categories_PropertyChanged);
            this.Categorias = CollectionViewSource.GetDefaultView(this._Categorias);
            this.Categorias.CurrentChanged += new EventHandler(Categorias_CurrentChanged);
            this.InvokePropertyChanged("Categorias");
        }

        void Categorias_CurrentChanged(object sender, EventArgs e)
        {
            this.DeleteCommand.RaiseCanExecuteChanged();
        }
        void Categories_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (this.Categorias.CurrentItem == null) return;
            var item = this.Categorias.CurrentItem as ExamCategory;
            item.MarkAsModified();            
            this.BCExams.MaintenanceExamCategories(new List<BusinessEntities.ExamCategory>(){ item});
            
        }        
        //[ExceptionCallHandler(Infrastructure.Aspect.Constants.ExceptionPolicyNames.PresenterExceptionPolicy)]        
        public virtual void Add(ExamCategory item)
        {
            StringBuilder  sb  =  null;
            if (!this.ValidationService.IsVisualObjectValid(this.View, out sb))
            {
                MessageBox.Show(sb.ToString());
                return;  
            }

            item.Id = Guid.NewGuid();
            this.BCExams.MaintenanceExamCategories(new List<BusinessEntities.ExamCategory>() { item });
            this.SyncContext.Send(C => this._Categorias.Add(item), null);
        }
        //[BlockAsyncExecution]
        //[AuthorizationCallHandler("AllowDeleteAuthor")]        
        public void Delete(ExamCategory item)
        {
            item.MarkAsDeleted();
            this.BCExams.MaintenanceExamCategories(new List<BusinessEntities.ExamCategory>() { item});
            this.SyncContext.Send(C => this._Categorias.Remove(item), null);
        }

    }
}
